#!/bin/bash
# by Su Zichao at 2021/04/13
# a script to run vecmap and evaluate the aligned embeddings by dictionary accuracy and cosine similarity
# Usage: ./vecmap_cossim.sh ${src} ${trg} path times gpu
# Example: ./vecmap_cossim.sh /disk/hlcao/zcsu/vecmap-master/data/embeddings/en.emb.txt /disk/hlcao/zcsu/vecmap-master/data/embeddings/it.emb.txt /disk/hlcao/zcsu/vecmap-master/data/dictionaries/en-it.test.txt 3 2 ./test1
# bash bachvecmap.sh 源语言 目标语言 结果保存目录 遍历次数（seed） GPU 结果保存的根目录
# bash bachvecmap.sh en zh test 3 1 baseline
set -e
if [ $# != 6 ] ; then exit
fi

startTime_s=`date +%s`
#src=$1 # src embedding path
#trg=$2 # trg embedding path
src=/disk/hlcao/data/muse/fasttext/wiki.$1.vec
trg=/disk/hlcao/data/muse/fasttext/wiki.$2.vec
dic1500=/disk/hlcao/data/muse/dictionaries/$1-$2.5000-6500.txt
dic5k=/disk/hlcao/data/muse/dictionaries/$1-$2.0-5000.txt
#workpath=/disk/hlcao/vecmap/experiments/$3
workpath=$6/$3
if [ ! -d ${workpath}  ];then
	  mkdir ${workpath}
fi
times=$4
gpu=$5 # cuda device number


output1=${workpath}/out1.s # aligned src emb path
output2=${workpath}/out2.s # aligned trg emb path
#dict=$3 # test dictionary path
outputfile=${workpath}/temp.log 
cosres=${workpath}/result.txt # cosine similarity result

#tool=myinit_map_embeddings.py
tool=map_embeddings.py

for((seed = 0; seed < ${times}; ++ seed))
do
    echo CUDA_VISIBLE_DEVICES=${gpu} python3 map_embeddings.py --unsupervised ${src} ${trg} ${output1}${seed} ${output2}${seed} --cuda --verbose --seed ${seed}
#    CUDA_VISIBLE_DEVICES=${gpu} python3 map_embeddings.py --unsupervised ${src} ${trg} ${output1}${seed} ${output2}${seed} --cuda --verbose --seed ${seed}
    #CUDA_VISIBLE_DEVICES=${gpu} python3 map_embeddings.py ${src} ${trg} ${output1}${seed} ${output2}${seed} --cuda --verbose --seed ${seed} --validation $dic1500 --supervised $dic5k 2> $workpath/log2${seed} >$workpath/log${seed}
    CUDA_VISIBLE_DEVICES=${gpu} python3 $tool ${src} ${trg} ${output1}${seed} ${output2}${seed} --cuda --seed ${seed} --unsupervised > $workpath/log2${seed} >$workpath/log${seed}
    echo CUDA_VISIBLE_DEVICES=${gpu} python3 eval_translation_max.py ${output1}${seed} ${output2}${seed} -d ${dict} --cuda --retrieval csls --output ${outputfile} --seed_in ${seed}
    CUDA_VISIBLE_DEVICES=${gpu} python3 eval_translation_max.py ${output1}${seed} ${output2}${seed} -d ${dic1500} --cuda --retrieval csls --output ${outputfile} --seed_in ${seed}
done

echo python3 max_cossim_cal.py ${outputfile} ${cosres}
python3 max_cossim_cal.py ${outputfile} ${cosres}

endTime_s=`date +%s`
seconds=$(($endTime_s-$startTime_s))
HMS=`echo $((${seconds}/3600))h $((${seconds}%3600/60))m $((${seconds}%3600%60))s`
echo total run time: $HMS
echo -e "\a"
rm $workpath/out*.s*
tail $workpath/temp.log
tail $workpath/result.txt
